package com.ruoyi.project.system.controller;

import com.ruoyi.framework.aspectj.lang.annotation.Anonymous;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysProduct;
import com.ruoyi.project.system.domain.SysUserProductPrice;
import com.ruoyi.project.system.mapper.SysProductMapper;
import com.ruoyi.project.system.service.ISysProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * 产品Controller
 */
@RestController
@RequestMapping("/product")
public class SysProductController extends BaseController {

    @Autowired
    private ISysProductService sysProductService;

    @Autowired
    private SysProductMapper sysProductMapper;

    @GetMapping("/getPricing")
    public AjaxResult getPricing(@NotNull Integer userId, @NotNull Integer productId) {
        SysProduct sysProduct = sysProductService.selectSysProductByProductId(productId);
        SysUserProductPrice userProductPrice = sysProductService.getUserProductPrice(userId, productId);
        return success(new SysProduct(sysProduct, userProductPrice));
    }

    @GetMapping("/getFee")
    public AjaxResult getFee(Integer userId, Integer productId) {
        return success(sysProductService.getFee(userId, productId));
    }

    /**
     * 获取产品状态
     */
    @Anonymous
    @GetMapping("/homeProducts")
    public AjaxResult homeProducts() {
        AjaxResult result = new AjaxResult();
        result.put("PTR", sysProductMapper.selectHomeProducts("PTR"));
        result.put("OTHER", sysProductMapper.selectHomeProducts("OTHER"));
        return result;
    }

    /**
     * 获取产品状态
     */
    @Anonymous
    @GetMapping("/status")
    public AjaxResult productStatus(int productId) {
        AjaxResult result = new AjaxResult();
        result.putAll(sysProductMapper.getProductStatus(productId));
        return result;
    }

    @GetMapping("/names")
    public AjaxResult productNameDistinct() {
        return success(sysProductMapper.productNameDistinct());
    }

    /**
     * 修改用户产品定制价格
     */
    @Log(title = "修改用户产品定制价格", businessType = BusinessType.UPDATE)
    @PutMapping("/setPrice")
    public AjaxResult setPrice(@RequestBody SysUserProductPrice supp) {
        SysUserProductPrice userProductPrice = sysProductMapper.customizedSiyuUserPrice(supp.getUserId(), supp.getProductId());
        if (userProductPrice == null) {
            return toAjax(sysProductMapper.insertSysUserProductPrice(supp));
        } else {
            return toAjax(sysProductMapper.updateSysUserProductPrice(supp));
        }
    }

    /**
     * 获取产品详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:product:query')")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") int productId) {
        return success(sysProductService.selectSysProductByProductId(productId));
    }

    /**
     * 修改产品
     */
    @PreAuthorize("@ss.hasPermi('system:product:edit')")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysProduct sysProduct) {
        return toAjax(sysProductService.updateSysProduct(sysProduct));
    }
}
